package com.starblink.frame.app.ui

import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.FragmentUtils
import com.blankj.utilcode.util.SizeUtils
import com.starblink.frame.app.adapter.MainCell
import com.starblink.frame.app.adapter.MainCell2
import com.starblink.frame.app.base.BaseActivity
import com.starblink.frame.app.vm.MainVM
import com.starblink.frame.app.databinding.ActivityMainBinding
import com.starblink.frame.app.databinding.CellMainItemBinding
import com.starblink.frame.app.myinterface.MainI
import com.starblink.frame.base.adapter.MutableAdapter
import com.starblink.frame.base.adapter.SimpleGapItemDecor

class MainActivity : BaseActivity<ActivityMainBinding, MainVM>(),MainI {

    var num : Int = 0
    lateinit var layoutManager: LinearLayoutManager
    private val mainAdapter by lazy {
        MutableAdapter(viewModel.mainDataList.value!!).apply {
            addVhDelegate {
                MainCell(CellMainItemBinding.inflate(layoutInflater,it,false),this@MainActivity)
            }
            addVhDelegate {
                MainCell2(it)
            }
        }
    }
    override fun initViewBinding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun initData() {
        viewModel.reqTestData()
    }

    override fun initView() {
        layoutManager= LinearLayoutManager(this)
        viewBinding.rvContent.layoutManager = layoutManager
        viewBinding.rvContent.adapter = mainAdapter
        viewBinding.rvContent.addItemDecoration(
            SimpleGapItemDecor(
                horGap = SizeUtils.dp2px(10.toFloat()),
                verGap = SizeUtils.dp2px(10.toFloat())
            )
        )
    }

    override fun initObservable() {
        viewModel.mainDataList.observe(this){
            mainAdapter.notifyDataSetChanged()
        }
    }

    override fun addHomeFragment() {
        BlankFragment.newInstance(num++).let{
            FragmentUtils.add(
                supportFragmentManager,
                it,
                viewBinding.flContent.id,
                "num$num",
                false,
                true
            )
            FragmentUtils.show(it)
        }
    }

    override fun subHomeFragment() {
        num --
    }
}